﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Sinacor.Infra.Tools.ErrorLogCollector
{
    public abstract partial class BaseMain : Form
    {
        public BaseMain()
        {
            InitializeComponent();

            txtFile.Text = Path.GetPathRoot(Application.StartupPath) + "LogPackage.zip";
        }

        private void btnCollect_Click(object sender, EventArgs e)
        {
            // Força o foco no botão para atualizar as datas
            btnCollect.Focus();

            DateTime from = dtFrom.Value.Date;
            DateTime to = dtTo.Value.Date;
            string path = null;

            if (from > to)
            {
                MessageBox.Show("A data 'De' deve ser menor ou igual a data 'Até'", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                dtFrom.Focus();
                return;
            }


            if (string.IsNullOrEmpty(txtFile.Text))
            {
                MessageBox.Show("Por favor informe um caminho", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtFile.Focus();
                return;
            }

            try
            {
                path = Path.GetFullPath(txtFile.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Caminho inválido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFile.Focus();
                return;
            }

            new Action<DateTime, DateTime, string>(AsyncCollect).BeginInvoke(from, to, path, null, null);
        }

        private void AsyncCollect(DateTime from, DateTime to, string path) 
        {
            try
            {
                this.Invoke(new Action(() => panel.Enabled = false));
                this.Invoke(new Action(() => this.Cursor = Cursors.WaitCursor));

                to = to.AddHours(23);
                to = to.AddMinutes(59);
                to = to.AddSeconds(59);

                this.Collect(from, to, path);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocorreu um erro ao gerar os logs\n\n" + ex.ToString(), "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Invoke(new Action(() => panel.Enabled = true));
                this.Invoke(new Action(() => this.Cursor = Cursors.Default));
            }
        }

        protected abstract void Collect(DateTime from, DateTime to, string path);

        protected void ShowStatus(string text)
        {
            this.Invoke(new Action(() => toolStripStatusLabel.Text = text));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();

            dialog.OverwritePrompt = true;

            dialog.DefaultExt = ".zip";
            dialog.Filter = "ZIP files (*.zip)|*.zip";

            dialog.FileName = txtFile.Text;

            DialogResult result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                txtFile.Text = dialog.FileName;
            }
        }
    }
}
